<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/25
 * Time: 11:27
 */
namespace Home\Aip;
use Lib\PasswordHash;
use Think\Controller;

class User extends Controller{
    private $table = 'user';
    public $actioninfo = '';//返回操作字符
    /**
     * 用户登录
     */
    function _login(){
        $user = M($this->table);
        $getUserName = I('post.uname');
        $getUserPwd = I('post.upwd');
        $getCode = I('post.code');	
		
		if (I('post.pm_sms')=="1") {
			$pm_sms = 'username';
		} else {
			if (I('post.pm_sms')){
				$pm_sms = 'phone';
			}			
		}
		switch($pm_sms){
			case 'username':
				if (!$getUserName || !$getUserPwd) {
				   return $this->actioninfo = "账号、密码不能为空！";          
				}
				$where ="username='%s'";
				$data  = array(I('post.uname'));
				$type[1] = '账号不存在！';
				$type[2] = '账号密码有误！';
			    $tyle = 1;
			break;
			case 'phone':
				if (!$getUserName) {
				   return $this->actioninfo = "手机号码不能为空！";          
				} else {					
					$mobile = $this->isMobile($getUserName);
					if (!$mobile) {
						return $this->actioninfo = '手机号格式错误！';
					}
				}
				$where ="mobile='%s'";
				$data  = array(I('post.uname'));
				$type[1] = '手机号码不存在！';
				$type[2] = '短信验证码错误！';
			    $tyle = 2;
			break;
		}
		
		$Userinfo = _goLogin($this->table,$where,$data);
		
		if (!$Userinfo) {
			return $this->actioninfo = $type[1];
		}
		if ($Userinfo==4) {
			//账号已登录，不需要重新登录
			return $this->actioninfo = 'ok';
		}
		
        if ($Userinfo['status']=='0') {
			return $this->actioninfo = "账号未开通！";
        } elseif ($Userinfo['status']=='2') {
			return $this->actioninfo = "账号已休眠！";
		} elseif ($Userinfo['status']=='3') {
			return $this->actioninfo = "已被冻结！";
		}
		
		if ($tyle==1) {
/* 			if ($Userinfo && !$Userinfo['shoppassword']) {
				$user_pwd = $Userinfo["password"];	
				if (MD5(I('post.upwd')) == $user_pwd) {
					$check = 1;
				} else {
					$check = 0;
				}
			} else {
				$user_pwd = $Userinfo["shoppassword"];
				$ph=new PasswordHash();
				$check = $ph->CheckPassword(MD5(I('post.upwd')),$user_pwd);
			} */
			
			if($Userinfo){
				$user_pwd = $Userinfo["password"];
				$ph=new PasswordHash();
				//dump($ph);die;
				$check = $ph->CheckPassword(MD5(I('post.upwd')),$user_pwd);				
			}
		
		} elseif ($tyle==2) {
			if ($Userinfo) {
				$sms = $this->sms_phone(I('post.pm_sms'));
				$ope_sms = 1;
				//开启短信验证
				if ($ope_sms) {
					if (!$sms) {
						return $this->actioninfo = "短信验证码错误！";
					} else {
						$check = 1;
					}				
				} else {
					$check = 1;
				}
			}			
		}
		       
        if (count($Userinfo)>0 && $check) {
            //登录成功，进行记录操作
			$this->login_sesion($Userinfo,$this->table,'id=%d',array($Userinfo['id']));			
            //日志记录
            return $this->actioninfo = 'boboid';
            
        }  else {
			return $this->actioninfo=$type[2];    
      
        }
    }
	
	
	/*
	*登录成功，进行记录操作
	*/
    function login_sesion($Userinfo,$table,$str,$arr){
		
			$data['current_ip'] = get_client_ip() ;
			$data['last_ip'] = $Userinfo['current_ip'] ;
			$data['last_time'] = $Userinfo['current_time'] ;
			$data['current_time'] = time() ;
			$edit_logim = _getEdit($table,$str,$arr,$data) ;
			session('boboid',$Userinfo['id']);
			session('user_id',$Userinfo['id']);
			session('phone',$Userinfo['mobile']);
			session('user_name',$Userinfo['username']);
			session('user_pid',$Userinfo['tusername']);	
			session('loginOver',time());//强制退出时间记录
			session('gotegary',$Userinfo['gotegary']);
			//测试专用
			if ($Userinfo['username']=="xxss") {
				setcookie('ceshi001','ceshi001');
			}
				
    }		
	
	/*
	*短信验证
	*/
    function sms_phone($sms_code){
		return 1;
    }
	
	
	/**
	* 验证手机号是否正确
	* @author honfei
	* @param number $mobile
	*/
	 function isMobile($mobile) {
		if (!is_numeric($mobile)) {
			return false;
		}
		return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
	 }
 

    /**
     * 用户注册
     */
    function _reg_user(){
        $user = M($this->table);
        $UserName = I('post.user_name');
        $UserPwd = I('post.password');
		
		if (!I('post.pm_sms')) {
			$pm_sms = 'username';
		} else {
			if (I('post.pm_sms')){
				$pm_sms = 'phone';
			}			
		}
		switch($pm_sms){
			case 'username':
				if (!$UserName || !$UserPwd) {
				   return $this->actioninfo = "账号、密码不能为空！";          
				}
				$where ="username='%s'";
				$data  = array(I('post.user_name'));
				$type[1] = '账号已存在！';
				$type[2] = '邮箱已存在！';
			    $tyle = 1;
				$where2 ="email='%s'";
				$data2  = array(I('post.email'));
				$uemail = _goLogin($this->table,$where2,$data2);
				if ($uemail) {
					return $this->actioninfo = $type[2];
				}
			break;
			case 'phone':
				if (!$getUserName) {
				   return $this->actioninfo = "手机号码不能为空！";          
				} else {					
					$mobile = $this->isMobile($getUserName);
					if (!$mobile) {
						return $this->actioninfo = '手机号格式错误！';
					}
				}
				$where ="mobile='%s'";
				$data  = array(I('post.user_name'));
				$type[1] = '手机号码已被注册！';
				$type[2] = '短信验证码错误！';
			    $tyle = 2;
			break;
		}
		
		$Userinfo = _goLogin($this->table,$where,$data);
		
		if ($Userinfo) {
			return $this->actioninfo = $type[1];
		}
		
		$user_pwd = _enPasswd(md5(I('post.password'))) ;
		
		unset($data_u);
		
		if ($tyle==1) {			
			$data_u ['username'] = I('post.user_name') ;
			$data_u ['email'] = I('post.email') ;			
			$check = 1;
		} elseif ($tyle==2) {
			if (!$Userinfo) {
				$sms = $this->sms_phone(I('post.pm_sms'));
				$ope_sms = 1;
				//开启短信验证
				if ($ope_sms) {
					if (!$sms) {
						return $this->actioninfo = "短信验证码错误！";
					} else {
						$check = 1;
					}				
				} else {
					$check = 1;
				}
				if ($check) {
					//手机注册 
				}
				
			}			
		}
		
		$data_u ['status'] = 1 ;
		$data_u ['password'] = $user_pwd ;
		
        if ($check) {
            $add = _getAdd($this->table,$data_u) ;
			if ($add) {
				return $this->actioninfo = 'boboid';
			} else {
				return $this->actioninfo = '会员注册失败！';
			}
        }  else {
			
			return $this->actioninfo=$type[2];    
        }
    }

    /**
     * 获取用户信息
     */
    function _getUserInfo($where,$data){		
        $Userinfo = _UserLogin($this->table,$where,$data);
        return $Userinfo;
    }
 

}
